package com.sdap2.dao;

import com.sdap2.common.dao.LogicDAO;
import com.sdap2.entity.SysUser;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Repository
public interface SysUserDAO extends LogicDAO<SysUser, Long> {
    
    @Query("SELECT u FROM SysUser u LEFT JOIN FETCH u.roles WHERE u.username = :username")
    Optional<SysUser> findByUsernameWithRoles(String username);
    
    Optional<SysUser> findByUsername(String username);
    
    boolean existsByUsername(String username);
    
    @Modifying
    @Transactional
    @Query(value = "INSERT INTO sys_user_role (user_id, role_id) VALUES (:userId, :roleId)", nativeQuery = true)
    void insertUserRole(@Param("userId") Long userId, @Param("roleId") Long roleId);
} 